<template>
  <el-row>
    <el-col :span="5" style="height: 100%; border-right: 1px solid #dfe6ec">
      <customTree :ancestralId="listQuery.ancestralId" :btnTitle="'创建治理规范根目录'" @getId="getId" @getTree="getTree" :type="false" :expandType="'1'" />
    </el-col>
    <el-col :span="19">
      <div class="app-container">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">标准名称:</span>
            <el-input v-model="listQuery.name" placeholder="请输入标准名称" style="width: 200px" />
          </div>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>

          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest') }}</el-button>

          <!-- <el-button v-waves v-permission="['standardsystem:ModelSpecification:add']" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
            {{ $t('table.add') }}
          </el-button>
          <el-button v-waves v-permission="['standardsystem:ModelSpecification:delete']" class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleBathDelete">
            {{ $t('table.delete') }}
          </el-button> -->
        </div>

        <el-table ref="table" :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" width="50" label="序号" align="center" />
          <el-table-column label="标准名称" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="标准版本号" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.version }}</span>
            </template>
          </el-table-column>
          <el-table-column label="标准发布时间" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布机构" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseInstitution }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布状态" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseStatus | dictLabel('res_status') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
            <template slot-scope="{ row }">
              <!-- <el-button v-permission="['standardsystem:ModelSpecification:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
                {{ $t('table.edit') }}
              </el-button> -->
              <el-button type="text" size="small" icon="el-icon-tickets" @click="handleUpdate(row)"> 详情 </el-button>
              <!--  <el-button type="text" size="small" icon="el-icon-edit" @click="stop(row)" v-if="row.releaseStatus == 0">
                废止
              </el-button>
              <el-button type="text" size="small" icon="el-icon-edit" @click="down(row)">
                下载
              </el-button>
              <el-button v-permission="['standardsystem:ModelSpecification:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
                {{ $t('table.delete') }}
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

        <governanceNormsForm ref="form" @refreshList="getList" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
// import { fetchMasterdataDirectoryList } from '@/api/masterdata/masterdataDirectory'
import { deleteGovernanceNorms, fetchGovernanceNormsList, updateGovernanceNorms, upDown, deleteTable, release } from '@/api/standardsystem/governanceNorms'

import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import governanceNormsForm from './governanceNormsForm'
import Pagination from '@/components/Pagination'

import customTree from '@/components/customTree/index.vue'
export default {
  name: 'ModelSpecificationList',
  components: { governanceNormsForm, Pagination, customTree },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      treeList: [],
      list: null,
      total: 0,
      multipleSelection: [],
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        name: undefined,
        ancestralId: '1',
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
      activeName: 'first', //tarbar
      NODE_KEY: 'id', // id对应字段
      MAX_LEVEL: 5, // 设定最大层级
      NODE_ID_START: 0, // 新增节点id，逐次递减
      startId: null,
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },

  methods: {
    // 树形列表
    getId(data) {
      this.listQuery.ancestralId = '1'
      this.listQuery.directoryId = data.id
      this.getList()
    },
    getTree(data) {
      this.treeList = data
    },

    load(tree, treeNode, resolve) {
      resolve(tree.children)
    },
    getListByTreeId(data) {
      this.listQuery.directory_id = data.id
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchGovernanceNormsList(this.listQuery).then((response) => {
        if (response.data.code == 0) {
          this.list = response.data.data
          this.total = response.data.total
          this.listLoading = false
        } else {
          this.$message.error(response.data.msg)
          this.listLoading = false
        }
      })
    },
    handleCreate() {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleUpdate(row.id)
    },
    publish(row) {
      this.$confirm('确定发布吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        release({ id: row.id, releaseStatus: 0 }).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success('发布成功')
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    stop(row) {
      this.$confirm('确定废止吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        release({ id: row.id, releaseStatus: 1 }).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success('废止成功')
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    down(row) {
      this.$confirm('确定下载所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        upDown({ ids: row.id }).then((response) => {
          // console.log(response,'1111')
          if (response.data.code == 200) {
            const objectUrl = response.data.data[0]
            const link = document.createElement('a')
            // 设置导出的文件名称
            // link.download = `治理规范` + '.xlsx'
            link.style.display = 'none'
            link.href = objectUrl
            link.click()
            document.body.appendChild(link)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteTable({ id: row.id, delFlag: 1 }).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteGovernanceNorms(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    //重置
    handleRest() {
      this.listQuery = {
        directoryId: this.listQuery.directoryId,
        page: this.listQuery.page,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
  },
}
</script>
<style lang="scss">
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

#treeBox {
  margin-top: 1px;

  .el-table thead {
    display: none;
  }
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
<style scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

.icon {
  width: 10px;
  height: 10px;
  /*background:url('../img/icon.jpg');*/
  background-size: 100% 100%;
}

.el-tree {
  width: 100% !important;
  min-width: auto !important;
}

.el-tree-node .is-current .is-focusable {
  background: rebeccapurple !important;
}
</style>
